package com.invite.me.wigets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import java.lang.ref.WeakReference;


/**
 * Created by HYK on 2017/3/24.
 */

@SuppressLint("AppCompatCustomView")
public class CountDownTextView extends TextView {
    private static final int MSG_POST_COUNT_DOWN = 233;

    private int allCountDownSecond = 60;
    private int getCodeInterval = 0;
    private String countDownEndText = "重新获取验证码";
    private String formater = "%ds";
    private OnCountDownListener onCountDownListener;
    @ColorInt
    private int countingTextColor;
    @ColorInt
    private int countEndTextColor;
    @ColorInt
    private int countingBgColor = -1;
    @ColorInt
    private int countEndBgColor = -1;

    public CountDownTextView(Context context) {
        this(context, null);
    }

    public CountDownTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountDownTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        countingTextColor = getCurrentTextColor();
        countEndTextColor = getCurrentTextColor();
    }

    private MyHandler mHandler = new MyHandler(this);

    public CountDownTextView setCountingTextColor(@ColorInt int countingTextColor) {
        this.countingTextColor = countingTextColor;
        return this;
    }

    public CountDownTextView setCountEndTextColor(@ColorInt int countEndTextColor) {
        this.countEndTextColor = countEndTextColor;
        return this;
    }

    public CountDownTextView setCountingBgColor(int countingBgColor) {
        this.countingBgColor = countingBgColor;
        return this;
    }

    public CountDownTextView setCountEndBgColor(int countEndBgColor) {
        this.countEndBgColor = countEndBgColor;
        return this;
    }

    private static class MyHandler extends Handler {
        private WeakReference<CountDownTextView> reference;

        public MyHandler(CountDownTextView textView) {
            reference = new WeakReference<>(textView);
        }

        @Override
        public void handleMessage(Message msg) {
            CountDownTextView textView = reference.get();
            if (textView == null) {
                return;
            }
            textView.getCodeInterval--;
            textView.setText(String.format(textView.formater, textView.getCodeInterval));
            if (textView.onCountDownListener != null) {
                textView.onCountDownListener.onCountDown(textView.allCountDownSecond, textView.getCodeInterval);
            }
            if (textView.getCodeInterval > 0) {
                sendEmptyMessageDelayed(MSG_POST_COUNT_DOWN, 1000);
            } else {
                textView.setText(textView.countDownEndText);
                textView.setEnabled(true);
                textView.setTextColor(textView.countEndTextColor);
                if (textView.countEndBgColor != -1) {
                    textView.setBackgroundColor(textView.countEndBgColor);
//                    textView.resetBackground();
                }
            }
        }
    }

    public void startCountDown() {
        getCodeInterval = allCountDownSecond;
        setEnabled(false);
        mHandler.sendEmptyMessageDelayed(MSG_POST_COUNT_DOWN, 0);
        setTextColor(countingTextColor);
        if (countingBgColor != -1) {
//            setBackColor(countingBgColor);
            setBackgroundColor(countingBgColor);
//            resetBackground();
        }
    }

    public void stopCountDown() {
        mHandler.removeMessages(MSG_POST_COUNT_DOWN);
    }

    public int getGetCodeInterval() {
        return getCodeInterval;
    }

    public CountDownTextView setCountDownEndText(String countDownEndText) {
        this.countDownEndText = countDownEndText;
        return this;
    }

    public String getCountDownEndText() {
        return countDownEndText;
    }

    public String getFormater() {
        return formater;
    }

    public CountDownTextView setFormater(String formater) {
        this.formater = formater;
        return this;
    }

    public CountDownTextView setOnCountDownListener(OnCountDownListener onCountDownListener) {
        this.onCountDownListener = onCountDownListener;
        return this;
    }

    public interface OnCountDownListener {
        void onCountDown(int allTime, int remain);
    }
}
